package roboticsfmw.sample;

import roboticsfmw.annotation.HardwareRef;
import roboticsfmw.common.ShutdownEvent;
import roboticsfmw.core.Event;
import roboticsfmw.core.Sensor;
import roboticsfmw.core.SensorException;

public class CommandSensor extends Sensor<UserIOHardwareProxy> {

	@HardwareRef(name="/hw/userIO")
	public UserIOHardwareProxy ioHw;
	
	public CommandSensor() throws IllegalArgumentException {
		// SensorName = Sample Command Sensor
		// polling interval = 250
		// Run Loop until turn off = true 
		super("Sample Command Sensor", 250, true);
	}

	@Override
	public void init() throws SensorException {
		ioHw.command="";
	}

	@Override
	public void shutdown() throws SensorException {
		ioHw.command=null;
	}

	@Override
	public void loop(long i) {
		if (!"".equals(ioHw.command) && ioHw.command!=null) {
			Event event = null;
			if ("shutdown".equalsIgnoreCase(ioHw.command)) {
				event = new ShutdownEvent("Robot is going shutdown!");
			} else {
				event = new CommandEvent(ioHw.command);
			}
			
			if (event!=null) {
				getEventBus().notify(event);
			}
			ioHw.command = "";
		}
	}

}
